<template>
	<view :style="viewColor">
		<view class='address-management' >
		
			<view class="address-main">
				<view class=""  v-if="passengerList.length">
					<view  class=" pa-item" v-for="(item, index) in passengerList" :key="index">
						<view class="row-center" style="  padding: 10px; border-bottom: 1px solid #eee;">
							<text style="font-size: 14px;color: #000000;font-weight: 600;width: 70px;">{{item.name}}</text>
							<text style="color: #909090;font-size: 12px;margin: 0 5px;margin-right: 25px;">{{item.id_card}}</text>
							<view class="idcard-tip" >
								{{item.type==1?'成人':'儿童'}}
							</view>
									
							
							</view>
							<view class="mt-10 display-flex">
								<view class='acea-row row-middle ml-auto operation'>
									<view @click="editPassenger(item)" ><text class='iconfont icon-ic_edit'></text>编辑</view>
									<view @click="deletePassenger(item.id,index)" ><text class='iconfont icon-ic_delete'></text>删除</view>
								</view>
							</view>
							<!-- <view class="row-center ml-auto">
								<text @click="editPassenger(item)"  class="iconfont icon-ic_edit " style="font-size: 25px;color: #007AFF;"></text>
								
								<view @click="deletePassenger(item.id,index)"   class="iconfont icon-ic_delete" style="margin-left: 12px;font-size: 25px;color: #ff0000;">
									
							</view> -->
						</view>
						<view v-if="index!=(passengerList.length-1)" class="separation-line" style="margin: 12px;">
							
						</view>
					</view>
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if="passengerList.length">
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
				<view class='noCommodity' v-if="!passengerList.length ">
					<view class='pictrue'>
						<image :src="`${domain}/static/images/noAddress.png`"></image>
					</view>
					<view>暂无出行人</view>
				</view>
			</view>
			<view class='footer acea-row row-between-wrapper'>
				<view class='addressBnt on' @click='addPa'><text class='iconfont icon-tianjiadizhi'></text>添加出行人</view>
			</view>
			<add-passenger  :display="showAddPassenger" :passenger="passenger"  @changeClose="closeAddPassenger"></add-passenger>
			
		</view>
		
	</view>
</template>

<script>
	import addPassenger from '../components/addPassenger/index.vue';

	import {
		getPassengerList,
		setAddressDefault,
		delAddress,
		editAddress,
		deletePassenger,
	} from '@/api/user.js';
	import { mapGetters } from "vuex";
	import { HTTP_REQUEST_URL } from '@/config/app';
	import { toLogin } from '@/libs/login.js';
	export default {
		components: {addPassenger},
		data() {
			return {
				passengerList: [],
				cartId: '',
				pinkId: 0,
				couponId: 0,
				loading: false,
				loadend: false,
				loadTitle: '加载更多',
				page: 1,
				limit: 20,
				domain: HTTP_REQUEST_URL,
				showAddPassenger: false,
				passenger: {},
			};
		},
		computed: mapGetters(['isLogin', 'viewColor']),
		onLoad(options) {
			if (this.isLogin) {
				this.cartId = options.cartId || '';
				this.pinkId = options.pinkId || 0;
				this.couponId = options.couponId || 0;
				this.getPassengerList(true);
			} else {
              toLogin()
			}
		},
		onReady() {

		},
		onShow: function() {
			let that = this;
			that.getPassengerList(true);
		},
		methods: {
			closeAddPassenger(){
				if (!this.isLogin) {
					 toLogin()
					 return
				} 
				this.showAddPassenger = false;
				this.getPassengerList(true);
			},
			addPa(){
				if (!this.isLogin) {
					 toLogin()
					 return
				} 
				this.passenger = {}
				this.showAddPassenger = true;
			},
			editPassenger(item){
				if (!this.isLogin) {
					 toLogin()
					 return
				} 
				this.passenger = item;
				this.showAddPassenger = true;
			},
			/**
			 * 获取地址列表
			 *
			 */
			getPassengerList: function(isPage) {
				let that = this;
				if (isPage) {
					that.loadend = false;
					that.page = 1;
					that.$set(that, 'passengerList', []);
				};
				if (that.loading) return;
				if (that.loadend) return;
				that.loading = true;
				that.loadTitle = '';
				getPassengerList({
					page: that.page,
					limit: that.limit
				}).then(res => {
					let list = res.data;
					let loadend = list.length < that.limit;
					that.passengerList = that.$util.SplitArray(list, that.passengerList);
					that.$set(that, 'passengerList', that.passengerList);
					that.loadend = loadend;
					that.loadTitle = loadend ? '哼~我也是有底线的' : '加载更多';
					that.page = that.page + 1;
					that.loading = false;
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},
			deletePassenger(id,index){
				var that = this
				uni.showModal({
				    title: '提示',
				    content: '确认删除出行人？',
					confirmText: '确认删除',
				    success: function (res) {
				        if (res.confirm) {
				            console.log('用户点击确定');
							deletePassenger({id: id}).then(res => {
								that.$util.Tips({
									title: '删除成功',
									icon: 'success'
								}, function() {
									that.passengerList.splice(index, 1);
									that.$set(that, 'passengerList', that.passengerList);
								});
							}).catch(err => {
								 that.$util.Tips(err.message)
							})
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			
			/**
			 * 编辑地址
			 */
			editAddress: function(id) {
				let cartId = this.cartId,
				pinkId = this.pinkId,
				couponId = this.couponId;
				this.cartId = '';
				this.pinkId = '';
				this.couponId = '';
				uni.navigateTo({
					url: '/pages/users/user_address/index?id=' + id + '&cartId=' + cartId + '&pinkId=' + pinkId + '&couponId=' +couponId
				})
			},
		
		
		},
		onReachBottom: function() {
			this.getPassengerList();
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep radio .wx-radio-input.wx-radio-input-checked,
	::v-deep uni-radio .uni-radio-input.uni-radio-input-checked {
	 border: 1px solid var(--view-theme)!important;
	 background-color: var(--view-theme)!important;
	}
	.address-main{
		padding: 0 20rpx;
	}
	.address-management {
		// background-color: #fff;
		// height: 100vh;
		padding-bottom: 100px;
	}
	.address-management .line {
		width: 100%;
		height: 3rpx;
	}
	.address-management .line image {
		width: 100%;
		height: 100%;
		display: block;
	}
	.address-management .item {
		padding: 0 30rpx;
		margin-top: 20rpx;
	}
	.address-management .item .address {
		padding: 30rpx 0;
		border-bottom: 1px solid #eee;
		font-size: 28rpx;
		color: #282828;
	}
	.address-management .item .address .consignee {
		font-size: 30rpx;
		margin-bottom: 20rpx;
	}
	.address-management .item .address .consignee .phone {
		margin-left: 25rpx;
	}
	.address-management .item .operation {
		height: 83rpx;
		font-size: 28rpx;
		color: #282828;
	}
	.b-color {
		background-color: var(--view-theme);
	}
	.address-management .item .operation .radio text {
		margin-left: 13rpx;
	}
	.operation .iconfont {
		font-size: 35rpx;
		vertical-align: -2rpx;
		margin-right: 10rpx;
	}
	.operation .iconfont.icon-ic_delete {
		margin-left: 54rpx;
		font-size: 38rpx;
	}
	 .footer {
		position: fixed;
		width: 100%;
		background-color: #fff;
		bottom: 0;
		bottom: calc(0rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		bottom: calc(0rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
		height: 106rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
	}
	.footer .iconfont {
		font-size: 35rpx;
		vertical-align: -2rpx;
		margin-right: 10rpx;
	}
	 .footer .addressBnt {
		width: 330rpx;
		height: 76rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 76rpx;
		font-size: 30rpx;
		color: #fff;
		background: linear-gradient( 270deg, var(--view-bntColor22) 0%, var(--view-bntColor21) 100%);
	}
	 .footer .addressBnt.on {
		width: 690rpx;
		margin: 0 auto;
	}
	 .footer .addressBnt .iconfont {
		font-size: 35rpx;
		margin-right: 8rpx;
		vertical-align: -1rpx;
	}
	 .footer .addressBnt.wxbnt {
		background-color: #fe960f;
	}
	.noCommodity{
		text-align: center;
		border-top: none;
		margin-top: 15vh;
	}
	.noCommodity .pictrue{
		height: 305rpx;
	}
	.noCommodity view{
		color: #999;
	}
	.pa-item{
		padding: 10px;
		background: #fff;
		border-radius: 10px;
		margin-top: 10px;
	}
	.pa-item:last-child {
	  padding-bottom: 120px;
	}
</style>
